//
//  DataManager.m
//  iFlick
//
//  Created by 张 霄鹤 on 10-2-4.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "DataManager.h"

static DataManager *managerInstance = nil;

@implementation DataManager

+ (DataManager *)sharedManager{
	if(managerInstance == nil){
		managerInstance = [[DataManager alloc] init];
	}
	return managerInstance;
}

- (id)init{
	if(self = [super init]){
		
	}
	return self;
}

/**
 * 保存对象到文件
 */
+ (BOOL)saveObjectToFile:(id)items withName:(NSString *)path{
	NSString *resourcePath =  [DataManager applicationDocumentsDirectory];
	NSString *plistPath = [resourcePath stringByAppendingPathComponent:path];
	BOOL bOk = [NSKeyedArchiver archiveRootObject:items toFile:plistPath];
	return bOk;
}

/**
 * 加载对象
 */
+ (id)loadObjectFromFile:(NSString *)path{
	NSString *resourcePath =  [DataManager applicationDocumentsDirectory];
	// 此处字符串 采用 快捷方式创建，不需要释放
	NSString *plistPath = [resourcePath stringByAppendingPathComponent:path];
	id iobj = [NSKeyedUnarchiver unarchiveObjectWithFile:plistPath];
	return iobj;
}

+ (BOOL)saveBinaryToFile:(NSData *)data withName:(NSString *)path{
	BOOL bret = NO;
	if(data && path){
		NSString *resourcePath =  [DataManager applicationDocumentsDirectory];
		NSString *target = [resourcePath stringByAppendingPathComponent:path];
		bret = [data writeToFile:target atomically:NO];
	}
	return bret;
}

+ (NSData *)loadBinaryFromFile:(NSString *)path{
	NSData *ret = nil;
	if(path){
		NSString *resourcePath =  [DataManager applicationDocumentsDirectory];
		NSString *target = [resourcePath stringByAppendingPathComponent:path];
		ret = [NSData dataWithContentsOfFile:target];
	}
	return ret;
}

#pragma mark -
#pragma mark AppDocumentPath

/**
 Returns the path to the application's documents directory.
 */
+ (NSString *)applicationDocumentsDirectory {
	// TODO 检查是否包含了 /
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

#pragma mark -
#pragma mark UserConfigStore

+ (void)setConfigInteger:(NSInteger)value forKey:(NSString *)key{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setInteger:value forKey:key];
}

+ (void)setConfigString:(NSString *)value forKey:(NSString *)key{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setObject:value forKey:key];
}

/**
 * 获取整数型设置
 * @param key NSString
 * @return NSInteger
 */
+ (NSInteger)getConfigInteger:(NSString *)key{
	NSInteger ret = -1;
	if (key) {
		NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
		ret = [userDefaults integerForKey:key];
	}
	return ret;
}

/**
 * 获取字符串型设置
 * @param key NSString
 *
 */
+ (NSString *)getConfigString:(NSString *)key{
	NSString *ret = nil;
	if (key) {
		NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
		ret = [userDefaults objectForKey:key];
	}
	return ret;
}

/**
 * 获取BOOL设置
 * @param key NSString* 名称
 * @return BOOL
 */
+ (BOOL)getConfigBool:(NSString *)key{
	BOOL bret = NO;
	if (key) {
		NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
		bret = [userDefaults boolForKey:key];
	}
	return bret;
}

/**
 * 设置BOOL类型属性
 * @param value BOOL 属性值 
 * @param key   NSString* 属性名称
 */
+ (void)setConfigBool:(BOOL)value forKey:(NSString *)key{
	if (key) {
		NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
		[userDefaults setBool:value forKey:key];
	}
}

+ (void)saveAll{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults synchronize];
}


#pragma mark -
#pragma mark MemoryManager

- (void)dealloc{

	[super dealloc];
}

@end
